module.exports = class extends think.Model {
  async adminmenu() {
    const menu = await think.cache('adminenu', () => {
      return this.getallmenu();
    }, {timeout: 365 * 24 * 3600});
    return menu;
  }

  async getallmenu(uid, is_admin) {
    const where = {}; // 初始化查询条件
    where.status = ['>', '-1'] // 查询status > -1
    if (!think.config('setup.DEVELOP_MODE')) { // 判断是不是开发模式
      where.is_dev = 0;
    }
    const groups = think.config('setup.MENU_GROUP') || {}; // 获取后台菜单
    console.log(groups);
    var arr = {};
    let menu = [];
    menu = await this.where(where).order('sort asc').select();
    if (!think.isEmpty(menu)) { // 判断获取到的数据是否是非空
      let nmenu = [];
      if (!is_admin) {
        for (const m of menu) {
          const auth = think.service('kms/rbac', uid);
          const res = await auth.check(m.url);
          if (res) {
            nmenu.push(m);
          }
        }
      } else {
        nmenu = menu;
      }
      arr = arr_to_tree(nmenu, 0);
    }
    return arr;
  }

  async getallmenu2table(uid, is_admin) {
    const where = {};
    if (!think.config('setup.DEVELOP_MODE')) {
      where.is_dev = 0;
    }
    const groups = think.config('setup.MENU_GROUP') || {};
    var arr = {};
    let menu = [];
    menu = await this.where(where).order('sort asc').select();
    if (!think.isEmpty(menu)) { // 判断获取到的数据是否是非空
      let nmenu = [];
      if (!is_admin) {
        for (const m of menu) {
          const auth = think.service('kms/rbac', uid);
          const res = await auth.check(m.url);
          if (res) {
            nmenu.push(m);
          }
        }
      } else {
        nmenu = menu;
      }
      arr = arr2table(nmenu);
    }
    return arr;
  }

  async topmenu() {
    const where = {};
    where.hide = 0;
    where.pid = 0;
    if (!think.config('setup.DEVELOP_MODE')) {
      where.is_dev = 0;
    }
    const menu = await this.where(where).order('sort asc').select();
    return menu;
  }

  async group(id) {
    const where = {};
    where.hide = 0;
    where.pid = id;
    if (this.config('setup.DEVELOP_MODE')) {
      where.is_dev = 0;
    }
    const groups = await this.where(where).field('group').order('sort asc').select();
    return groups;
  }

  async getmenu(uid, is_admin) {
    const where = {};
    if (!think.config('setup.DEVELOP_MODE')) {
      where.is_dev = 0;
    }
    const groups = think.config('setup.MENU_GROUP') || {};
    var arr = {};
    let menu = [];
    menu = await this.where(where).order('sort ASC, id ASC').select();
    if (!think.isEmpty(menu)) { // 判断获取到的数据是否是非空
      let nmenu = [];
      if (!is_admin) {
        for (const m of menu) {
          const auth = think.service('kms/rbac', uid);
          const res = await auth.check(m.url);
          if (res) {
            nmenu.push(m);
          }
        }
      } else {
        nmenu = menu;
      }
      arr = arr2table(nmenu);
    }
    return arr;
  }
};
